/**
 * @author wangning 20180414
 * 实时安全状态
 */
import { Component, OnInit } from '@angular/core';
import { HttpServcie } from '../../../../service/http.service';
import {NzNotificationService} from "ng-zorro-antd";

@Component({
    selector: 'current-time-state',
    templateUrl: './current-time-state.html',
    styleUrls: ['./current-time-state.scss']
})

export class CurrentTimeState implements OnInit {

    partyId: any = JSON.parse(window.localStorage.getItem('user')).partyId;

    // 单位等级
    partyLevel: any = JSON.parse(window.localStorage.getItem("user")).partyLevel;

    partyParentId:any =  JSON.parse(window.localStorage.getItem("user")).partyParentId;

    option5 = {};//电梯安全保障工作落实情况图表
    option6 = {};//电梯事故隐患排除情况
    option7 = {};//超期情况统计

    dayFeedback;//当日反馈数
    quarterRescue;//当季度反馈数
    dayRescue;//当日救援数
    quarterFeedback;//当季度救援数


    dayMaintNum;//当日维保数
    quarterMaintNum;//当季度维保数
    yaerMaintNum;//当年度维保数
    dayInspNum;//当日巡查数
    quarterInspNum;//当季度巡查数
    yearInspNum;//当年度巡查数
    table = [];
    countRepairRet = [];
    CountMaintRet = [];

    constructor(
        private httpServcie: HttpServcie,
        private _notification: NzNotificationService
    ) {

    }
    ngOnInit() {
        //辖区内各项安全状况统计      
        this.countSafeNum();
        //电梯安全保障工作落实情况
        this.countMaintRepairNum();
        //电梯事故隐患排除情况
        this.countRepairNum();
        //电梯安全保障工作超期情况
        this.statCountMaintNum();
    }

    //辖区内各项安全状况统计
    countSafeNum() {
        this.httpServcie.countSafeNum({}).subscribe(data => {
            if (data.data) {
                let ret = data.data;
                this.dayFeedback = ret.dayFeedback;
                this.quarterRescue = ret.quarterRescue;
                this.dayRescue = ret.dayRescue;
                this.quarterFeedback = ret.quarterFeedback;
            }
        })
    }

    //电梯安全保障工作落实情况
    countMaintRepairNum() {
        if (this.partyLevel == null || this.partyLevel == undefined) {
          this._notification.create("warning", "提示", "等级不能为空，请联系超管");
          return;
        }

        if (this.partyLevel == 3) {
          this.partyId = this.partyParentId;
        }

        this.httpServcie.countMaintRepairNum({"id":this.partyId,"partyLevel":this.partyLevel}).subscribe(_data => {
            if (_data.data) {
                let ret = _data.data;
                this.table = ret;
                //当日工作落实
                let dayWorkArr = [];
                //本季度累计落实
                let quarterWorkArr = [];
                //本年度累计落实
                let yearWorkArr = [];

                /*假数据*/
                // 定义每天巡检数据
                let dayCheck:any=0;
                // 定义本季度巡检数据
                let quarterCheck:any=0;
                // 定义本年度巡检数据
                let yearCheck:any=0;


                this.dayMaintNum = ret.dayMaintNum;
                this.quarterMaintNum = ret.quarterMaintNum;
                this.yaerMaintNum = ret.yaerMaintNum;

                this.dayInspNum = ret.dayInspNum;
                this.quarterInspNum = ret.quarterInspNum;
                this.yearInspNum = ret.yearInspNum;


                dayWorkArr.unshift(dayCheck);
                dayWorkArr.unshift(this.dayMaintNum);
                dayWorkArr.unshift(this.dayInspNum);

                quarterWorkArr.unshift(quarterCheck);
                quarterWorkArr.unshift(this.quarterMaintNum);
                quarterWorkArr.unshift(this.quarterInspNum);

                yearWorkArr.unshift(yearCheck);
                yearWorkArr.unshift(this.yaerMaintNum);
                yearWorkArr.unshift(this.yearInspNum);


                this.option5 = {
                    tooltip : {
                      trigger: 'axis'
                    },
                    legend: {
                        data: ['当日工作落实', '本季度累计落实', '本年度累计落实']
                    },
                    color:['#A5D1F0', '#D3C9E7','#92DEDF'],
                    toolbox: {
                      show : true,
                      feature : {
                        mark : {show: true},
                        dataView : {show: true, readOnly: false},
                        magicType : {show: true, type: ['line', 'bar', 'stack', 'tiled']},
                        restore : {show: true},
                        saveAsImage : {show: true}
                      }
                    },
                    calculable : true,
                    xAxis : [
                      {
                        type : 'category',
                        boundaryGap : false,
                        data : ['安全巡查', '日常维保', '定期检验']
                      }
                    ],
                    yAxis : [
                      {
                        type : 'value'
                      }
                    ],
                    series: [
                        {
                            name: '当日工作落实',
                            type:'line',
                            smooth:true,
                            itemStyle: {normal: {areaStyle: {type: 'default'}}},
                            data: dayWorkArr
                        },
                        {
                            name: '本季度累计落实',
                            type:'line',
                            smooth:true,
                            itemStyle: {normal: {areaStyle: {type: 'default'}}},
                            data: quarterWorkArr
                        },
                        {
                            name: '本年度累计落实',
                            type:'line',
                            smooth:true,
                            itemStyle: {normal: {areaStyle: {type: 'default'}}},
                            data: yearWorkArr
                        }
                    ]
                };
            }
        })
    }

    //电梯事故隐患排除情况
    countRepairNum() {
        this.httpServcie.countRepairNum(this.partyId).subscribe(_data => {
            if (_data.data) {
                let ret = _data.data;

                console.log("XXXXXXXXXX");
                console.log(ret);
              console.log("XXXXXXXXXX");


                this.countRepairRet = JSON.parse(JSON.stringify(ret));
                this.countRepairRet.unshift({ dayRepairNum: '当日排除故障数', quarterRepairNum: '当季度排除故障数', yearRepairNum: '当年度排除故障数', partyName: '\\' });
                let partyNameArr = [];
                let dayRepairArr = [];
                let quarterRepairArr = [];
                let yearRepairArr = [];
                let partyNameShorthand = [];

                for (let i of ret) {
                    partyNameArr.push(i.partyName);
                    dayRepairArr.push(i.dayRepairNum);
                    quarterRepairArr.push(i.quarterRepairNum);
                    yearRepairArr.push(i.yearRepairNum);

                  if(i.newPartyName !=undefined && i.newPartyName !=null){
                    partyNameShorthand.push(i.newPartyName);
                  }
                }
                if(partyNameShorthand.length ==0){
                  partyNameShorthand = partyNameArr;
                }

                this.option6 = {
                    tooltip : {
                      trigger: 'axis'
                    },
                    legend: {
                        data: ['当日排除故障数', '当季度排除故障数', '当年度排除故障数']
                    },
                    color:['#E4B5B8', '#D3C9E7','#92DEDF'],
                    toolbox: {
                      show : true,
                      feature : {
                        mark : {show: true},
                        dataView : {show: true, readOnly: false},
                        magicType : {show: true, type: ['line', 'bar', 'stack', 'tiled']},
                        restore : {show: true},
                        saveAsImage : {show: true}
                      }
                    },
                    calculable : true,
                    xAxis : [
                      {
                        type : 'category',
                        boundaryGap : false,
                        data : partyNameShorthand
                      }
                    ],
                    yAxis : [
                      {
                        type : 'value'
                      }
                    ],
                    series: [
                        {
                            name: '当日排除故障数',
                            type:'line',
                            stack: '总量',
                            itemStyle: {normal: {areaStyle: {type: 'default'}}},
                            data: dayRepairArr,
                        },
                        {
                            name: '当季度排除故障数',
                            type:'line',
                            stack: '总量',
                            itemStyle: {normal: {areaStyle: {type: 'default'}}},
                            data: quarterRepairArr,
                        },
                        {
                            name: '当年度排除故障数',
                            type:'line',
                            stack: '总量',
                            itemStyle: {normal: {areaStyle: {type: 'default'}}},
                            data: yearRepairArr,
                        },
                    ],
                };
            }
        })
    }

    //电梯安全保障工作超期情况
    statCountMaintNum() {
      if (this.partyLevel == null || this.partyLevel == undefined) {
        this._notification.create("warning", "提示", "等级不能为空，请联系超管");
        return;
      }

      if (this.partyLevel == 3) {
        this.partyId = this.partyParentId;
      }
      this.httpServcie.statCountMaintNum({"id":this.partyId,"partyLevel":this.partyLevel}).subscribe(_data => {
          if (_data.data) {
              let ret = _data.data;
              this.CountMaintRet = JSON.parse(JSON.stringify(ret));
              this.CountMaintRet.unshift({ dayCheckNum: '定期检验当日超期', quarterCheckNum: '定期检验本季度超期', yearCheckNum: '定期检验本年度超期', dayMaintNum: '日常维保当日超期', quarterMaintNum: '日常维保本季度超期', yearMaintNum: '日常维保本年度超期', partyName: '\\' });

              let partyNameArr = [];
              let dayMaintArr = [];
              let quarterMaintArr = [];
              let yearMaintArr = [];

              // 假数据
              let dayCheckArr = [];
              let quarterCheckArr = [];
              let yearCheckArr = [];

              let partyNameShorthand = [];

              for (let i of ret) {
                  partyNameArr.push(i.partyName);
                  dayMaintArr.push(i.dayMaintNum);
                  quarterMaintArr.push(i.quarterMaintNum);
                  yearMaintArr.push(i.yearMaintNum);

                  // 假数据
                  dayCheckArr.push(i.dayCheckNum);
                  quarterCheckArr.push(i.quarterCheckNum);
                  yearCheckArr.push(i.yearCheckNum);


                if(i.newPartyName !=undefined && i.newPartyName !=null){
                  partyNameShorthand.push(i.newPartyName);
                }
                if(partyNameShorthand.length ==0){
                  partyNameShorthand = partyNameArr;
                }
              }
              this.option7 = {
                  tooltip : {
                    trigger: 'axis'
                  },
                  legend: {
                    data:['维保当日超期','维保本季度超期','维保本年度超期','检验当日超期','检验本季度超期','检验本年度超期']
                  },
                  toolbox: {
                    show : true,
                    feature : {
                      dataView : {show: true, readOnly: false},
                      magicType : {show: true, type: ['line', 'bar', 'stack', 'tiled']},
                      restore : {show: true},
                      saveAsImage : {show: true}
                    }
                  },
                  color:['#C6D0EE', '#CDA1AA','#E6AD95F','#E9C4FC','#C3F6B9','#6CB7CC'],
                  calculable : true,
                  xAxis : [
                    {
                      type : 'category',
                      boundaryGap : false,
                      data : partyNameShorthand
                    }
                  ],
                  yAxis : [
                    {
                      type : 'value'
                    }
                  ],
                  series: [
                      {
                          name: '维保本年度超期',
                          type:'line',
                          stack: '总量',
                          itemStyle: {normal: {areaStyle: {type: 'default'}}},
                          data: yearMaintArr,
                      },
                      {
                          name: '维保本季度超期',
                          type:'line',
                          stack: '总量',
                          itemStyle: {normal: {areaStyle: {type: 'default'}}},
                          data: quarterMaintArr,
                      },
                      {
                          name: '维保当日超期',
                          type:'line',
                          stack: '总量',
                          itemStyle: {normal: {areaStyle: {type: 'default'}}},
                          data: dayMaintArr,
                      },
                      {
                          name: '检验当日超期',
                          type:'line',
                          stack: '总量',
                          itemStyle: {normal: {areaStyle: {type: 'default'}}},
                          data: dayCheckArr,
                      },
                      {
                          name: '检验本季度超期',
                          type:'line',
                          stack: '总量',
                          itemStyle: {normal: {areaStyle: {type: 'default'}}},
                          data: quarterCheckArr,
                      },
                      {
                          name: '检验本年度超期',
                          type:'line',
                          stack: '总量',
                          itemStyle: {normal: {areaStyle: {type: 'default'}}},
                          data: yearCheckArr,
                      },
                  ],
              };
          }
      })
    }
}
